package homework.printyourself.info.items;

import homework.printyourself.info.BaseObject;
import homework.util.db.DBEngine;
import homework.valid.validators.ValidateChain;
import homework.valid.validators.impl.InDBValidator;
import homework.valid.validators.impl.RegularValidator;

import java.io.IOException;

public class ChinaIDCardNumber extends BaseInformationItem {

    static {
        chain = new ValidateChain();
        chain.addValidator(new RegularValidator("身份证号格式错误", "[1-8]\\d{16}[0-9X]"));
        DBEngine engine;
        try {
            engine = DBEngine.fromPropertiesFile("db.properties");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        chain.addValidator(new InDBValidator("错误地区码", engine,"idcard_district", "id") {

            @Override
            public boolean isValid(Object v) {
                return super.isValid(((String)v).substring(0, 6));
            }
        });
    }

    public ChinaIDCardNumber(BaseObject obj, String value) {
        super(obj, value);
        itemName = "身份证号";
    }

    public ChinaIDCardNumber(BaseObject obj) throws IOException {
        super(obj, null);
        itemName = "身份证号";
        itemValue = (String)fi.input("请输入身份证号: ", chain);
    }
}
